﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class OrderProductDAO
    {

        #region general

        private static OrderProduct LoadObj(IDataReader datareader)
        {
            OrderProduct obj = new OrderProduct();
            obj.OPID = MpDataHelp.GetInt(datareader, "OPID");
            obj.ProductID = MpDataHelp.GetInt(datareader, "ProductID");
            obj.OrderID = MpDataHelp.GetInt(datareader, "OrderID");
            obj.Price = MpDataHelp.GetDouble(datareader, "Price");
            obj.Quantily = MpDataHelp.GetInt(datareader, "Quantily");
            obj.Total = MpDataHelp.GetDouble(datareader, "Total");
            return obj;
        }

        #endregion

        public static int OrderProduct_Add(OrderProduct obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_OrderProduct_Add");
            db.AddOutParameter(dbCommand, "OPID", DbType.Int32, obj.OPID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, obj.OrderID);
            db.AddInParameter(dbCommand, "Price", DbType.Double, obj.Price);
            db.AddInParameter(dbCommand, "Quantily", DbType.Int32, obj.Quantily);
            db.AddInParameter(dbCommand, "Total", DbType.Double, obj.Total);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@OPID"));
            return -1;
        }
        public static int OrderProduct_Update(OrderProduct obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_OrderProduct_Update");
            db.AddInParameter(dbCommand, "OPID", DbType.Int32, obj.OPID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, obj.OrderID);
            db.AddInParameter(dbCommand, "Price", DbType.Double, obj.Price);
            db.AddInParameter(dbCommand, "Quantily", DbType.Int32, obj.Quantily);
            db.AddInParameter(dbCommand, "Total", DbType.Double, obj.Total);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int OrderProduct_Delete(int OPID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_OrderProduct_Delete");
            db.AddInParameter(dbCommand, "OPID", DbType.Int32, OPID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<OrderProduct> OrderProduct_Select(int OPID, int ProductID, int OrderID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_OrderProduct_Select");
            db.AddInParameter(dbCommand, "OPID", DbType.Int32, OPID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "OrderID", DbType.Int32, OrderID);
            List<OrderProduct> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<OrderProduct>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
